﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Web.Configuration;
using log4net;
using TheBullysStore.Models.ModuloAdministracion.ProvinciaPeru;

namespace TheBullysStore.Models.ModuloAdministracion.DistritoPeru
{
    public class DistritoPeruDAO
    {
        public static String cadenaDB = WebConfigurationManager.ConnectionStrings["TheBullysStore"].ConnectionString;
        private static ILog log = LogManager.GetLogger(typeof(DistritoPeruDAO));

        public List<DistritoPeruBean> ListarDistritoPeru(ProvinciaPeruBean provincia)
        {
            SqlConnection sqlCon = null;
            try
            {
                List<DistritoPeruBean> listaDistritoPeru = new List<DistritoPeruBean>();

                sqlCon = new SqlConnection(cadenaDB);

                sqlCon.Open();

                string commandString = "SELECT * FROM Distrito " +
                                       "WHERE Id_Provincia = " + provincia.IdProvincia;

                SqlCommand sqlCmd = new SqlCommand(commandString, sqlCon);

                SqlDataReader dataReader = sqlCmd.ExecuteReader();

                while (dataReader.Read())
                {
                    DistritoPeruBean DistritoPeru = new DistritoPeruBean();
                    DistritoPeru.Nombre = Convert.ToString(dataReader["Nombre"]);
                    DistritoPeru.IdDistrito = Convert.ToInt32(dataReader["Id_Distrito"]);
                    DistritoPeru.IdProvincia = Convert.ToInt32(dataReader["Id_Provincia"]);
                    listaDistritoPeru.Add(DistritoPeru);
                }
                dataReader.Close();

                return listaDistritoPeru;
            }
            catch (Exception ex)
            {
                log.Error("listarDistritoPeru(EXCEPTION): ", ex);
                throw ex;
            }
            finally
            {
                sqlCon.Close();            
            }
        }
    }
}